package com.anxin.service.impl;

import com.anxin.entity.Doctor;
import com.anxin.entity.Schedule;
import com.anxin.mapper.DoctorMapper;
import com.anxin.mapper.ScheduleMapper;
import com.anxin.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public List<Doctor> getAllDoctors() {
        return doctorMapper.selectList(null);
    }

    @Override
    public Doctor getDoctorById(String id) {
        return doctorMapper.selectById(id);
    }

    @Override
    public void addDoctor(Doctor doctor) {
        doctorMapper.insert(doctor);
    }

    @Override
    public void updateDoctor(Doctor doctor) {
        doctorMapper.updateById(doctor);
    }

    @Override
    public void deleteDoctor(String id) {
        doctorMapper.deleteById(id);
    }

    @Override
    public List<Doctor> getAllDoctorsWithSchedules() {
        List<Doctor> doctors = doctorMapper.selectList(null);
        for (Doctor doctor : doctors) {
            List<Schedule> schedules = scheduleMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Schedule>().eq("doctor_id", doctor.getId())
            );
            doctor.setSchedules(schedules);
        }
        return doctors;
    }
}
